package com.zzj.hotel.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.zzj.hotel.core.entity.Article;
import com.zzj.hotel.core.entity.Classify;
import com.zzj.hotel.core.entity.Room;
import com.zzj.hotel.core.entity.User;
import com.zzj.hotel.core.utils.AjaxUtil;
import com.zzj.hotel.core.utils.IdManager;
import com.zzj.hotel.service.ClassifyService;
import com.zzj.hotel.service.UserService;

@Controller
@RequestMapping(value={"/user"})
public class UserController {
	@Autowired UserService userService;
	@Autowired ClassifyService classifyService;
	@RequestMapping(value={"/login"}, method=RequestMethod.GET)
	public ModelAndView toLogin(HttpServletRequest request, Model model){
		model.addAttribute("errorMsg", request.getParameter("errorMsg"));
		return new ModelAndView("admin/login", "command", new User());
	}
	@RequestMapping(value={"/login"}, method=RequestMethod.POST)
	public String login(@ModelAttribute("admin/login") User user,HttpServletRequest request, Model model){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User u = userService.findOne(username, password);
		if(u != null && "lCMub6szRf4qFRWo8INcqp6kBAThRRTB".equals(u.getUser_id())){
			request.getSession().setAttribute("userId", u.getUser_id());
			request.getSession().setAttribute("userName", u.getUser_name());
			return "redirect:../view/admin/main.jsp";
		}
		String errorMsg = "error!!!";
		
		return "redirect:login?errorMsg="+errorMsg;
	}
	@RequestMapping(value={"/userlogin"}, method=RequestMethod.POST)
	public String userlogin(HttpServletRequest request, Model model){
		String username = request.getParameter("username");
		String password = request.getParameter("passwd");
		User u = userService.findOne(username, password);
		if(u != null && !"".equals(u.getUser_id())){
			request.getSession().setAttribute("userId", u.getUser_id());
			request.getSession().setAttribute("userName", u.getUser_name());
			return "redirect:../view/index.jsp";
		}
		String errorMsg = "error!!!";
		
		return "redirect:../view/index.jsp?errorMsg="+errorMsg;
	}
	@RequestMapping(value={"/logout"}, method=RequestMethod.POST)
	public String logout(HttpServletRequest request, Model model){
		request.getSession().removeAttribute("userId");
		request.getSession().removeAttribute("userName");
		return "redirect:../view/index.jsp";
	}
	@RequestMapping(value={"/checkuser"}, method=RequestMethod.POST)
	public void checkuser(HttpServletRequest request,HttpServletResponse response){
		String username = request.getParameter("userName");
		User u = userService.findOne(username);
		String msg = "";
		boolean flag = true;
		if(u != null && !"".equals(u.getUser_id())){
			msg = username+":该用户名已被使用,请更换！";
			flag = false;
		}else{
			msg = "恭喜！"+username+" 可以使用！";
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(flag+"", msg);
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
	@RequestMapping(value={"/adduser"}, method=RequestMethod.POST)
	public void adduser(HttpServletRequest request,HttpServletResponse response){
		String username = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		User u = new User();
		u.setUser_id(IdManager.getId());
		u.setUser_name(username);
		u.setUser_pass(passWord);
		userService.addUser(u);
		AjaxUtil.renderData(response,"true");
	}
	@RequestMapping(value={"/getuser"}, method=RequestMethod.POST)
	public void getuser(HttpServletRequest request,HttpServletResponse response){
		String userId = request.getParameter("userId");
		User u = userService.findById(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		List<Classify> classifyList = classifyService.findList("AHvltSuH1IrHOtoX5SK2HlFNZ0syJMgB");//游记的分类id AHvltSuH1IrHOtoX5SK2HlFNZ0syJMgB
		for(Classify classify:classifyList){
			map.put(classify.getClassify_id(), classify);
		}
		map.put("user", u);
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
	@RequestMapping(value={"/updateuser"}, method=RequestMethod.POST)
	public void updateuser(HttpServletRequest request,HttpServletResponse response){
		String userId = request.getParameter("userId");
		String userAge = request.getParameter("userAge");
		String userSex = request.getParameter("userSex");
		String userEmail = request.getParameter("userEmail");
		String userTel = request.getParameter("userTel");
		User u = userService.findById(userId);
		u.setUser_age(userAge);
		u.setUser_sex(userSex);
		u.setUser_mail(userEmail);
		u.setUser_tel(userTel);
		userService.updateUser(u);
		Map<String,Object> map = new HashMap<String,Object>();
		List<Classify> classifyList = classifyService.findList("AHvltSuH1IrHOtoX5SK2HlFNZ0syJMgB");//游记的分类id AHvltSuH1IrHOtoX5SK2HlFNZ0syJMgB
		for(Classify classify:classifyList){
			map.put(classify.getClassify_id(), classify);
		}
		map.put("user", u);
		String jsonResult = JSON.toJSONString(map);
		AjaxUtil.renderData(response,jsonResult);
	}
}
